{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06e756ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pygame, sys\n",
    "from pygame.locals import *\n",
    "\n",
    "\n",
    "pygame.init()\n",
    "screen = pygame.display.set_mode((400, 300))\n",
    "pygame.display.set_caption('Hello World!')\n",
    "while True:\n",
    "    sysFont = pygame.font.SysFont(\"None\", 19)\n",
    "    rendered = sysFont.render ('Hello World', 0, (255, 100, 100))\n",
    "    screen.blit(rendered, (100, 100))\n",
    "    for event in pygame.event.get():\n",
    "        if event.type == QUIT:\n",
    "            pygame.quit()\n",
    "            sys.exit()\n",
    "    pygame.display.update()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "912a953f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8df35f8c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c9030adf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pygame 2.0.1 (SDL 2.0.14, Python 3.9.2)\n",
      "Hello from the pygame community. https://www.pygame.org/contribute.html\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "34"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pygame, sys\n",
    "from pygame.locals import *\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "clock = pygame.time.Clock()  #创建一个Pygame的Clock对象\n",
    "img = pygame.image.load('cat.jpg')  #导入这张猫图片\n",
    "\n",
    "#我们将定义一些数组来储存动画中图片的位置坐标。既然对象可以被移动，那么应该有\n",
    "#四个方向——上、下、左、右。每一个方向上都有40个等距的步长。我们将各方向上的值全部初\n",
    "#始化为0\n",
    "steps = np.linspace(20, 360, 40).astype(int)\n",
    "right = np.zeros((2, len(steps)))\n",
    "down = np.zeros((2, len(steps)))\n",
    "left = np.zeros((2, len(steps)))\n",
    "up = np.zeros((2, len(steps)))\n",
    "\n",
    "#设置图片的位置坐标是一件很烦琐的事情。不过，有一个小技巧可以用上——[::-1]可\n",
    "#以获得倒序的数组元素。\n",
    "right[0] = steps\n",
    "right[1] = 20\n",
    "down[0] = 360\n",
    "down[1] = steps\n",
    "left[0] = steps[::-1]\n",
    "left[1] = 360\n",
    "up[0] = 20\n",
    "up[1] = steps[::-1]\n",
    "\n",
    "#四个方向的路径可以连接在一起，但需要先用T操作符对数组进行转置操作，使得它们以\n",
    "#正确的方式对齐\n",
    "pos = np.concatenate((right.T, down.T, left.T, up.T))\n",
    "\n",
    "#在主循环中，我们设置时钟周期为每秒30帧\n",
    "clock.tick(30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6271cbe3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pygame, sys\n",
    "from pygame.locals import *\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "pygame.init()\n",
    "clock = pygame.time.Clock()  #创建一个Pygame的Clock对象\n",
    "screen = pygame.display.set_mode((400, 400))\n",
    "\n",
    "pygame.display.set_caption('Animating Objects')\n",
    "img = pygame.image.load('cat.jpg')  #导入这张猫图片\n",
    "\n",
    "#我们将定义一些数组来储存动画中图片的位置坐标。既然对象可以被移动，那么应该有\n",
    "#四个方向——上、下、左、右。每一个方向上都有40个等距的步长。我们将各方向上的值全部初\n",
    "#始化为0\n",
    "steps = np.linspace(20, 360, 40).astype(int)\n",
    "right = np.zeros((2, len(steps)))\n",
    "down = np.zeros((2, len(steps)))\n",
    "left = np.zeros((2, len(steps)))\n",
    "up = np.zeros((2, len(steps)))\n",
    "\n",
    "#设置图片的位置坐标是一件很烦琐的事情。不过，有一个小技巧可以用上——[::-1]可\n",
    "#以获得倒序的数组元素。\n",
    "right[0] = steps\n",
    "right[1] = 20\n",
    "down[0] = 360\n",
    "down[1] = steps\n",
    "left[0] = steps[::-1]\n",
    "left[1] = 360\n",
    "up[0] = 20\n",
    "up[1] = steps[::-1]\n",
    "\n",
    "#四个方向的路径可以连接在一起，但需要先用T操作符对数组进行转置操作，使得它们以\n",
    "#正确的方式对齐\n",
    "pos = np.concatenate((right.T, down.T, left.T, up.T))\n",
    "i = 0\n",
    "\n",
    "while True:\n",
    "    screen.fill((255, 255, 255))  # 清屏\n",
    "    if i >= len(pos):\n",
    "        i = 0\n",
    "    screen.blit(img, pos[i])\n",
    "    i += 1\n",
    "    for event in pygame.event.get():\n",
    "        if event.type == QUIT:\n",
    "            pygame.quit()\n",
    "            sys.exit()\n",
    "    pygame.display.update()\n",
    "\n",
    "#在主循环中，我们设置时钟周期为每秒30帧\n",
    "    clock.tick(30)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
